package com.bodacn.keypoint;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

/**
 * Created by bodacn on 2017-12-25.
 */


/*
* 加左右
*
* */
@SuppressLint("NewApi")
public class PictureTagLayout extends RelativeLayout implements View.OnClickListener {
    private static final int CLICKRANGE = 5;
    int startX ;
    int startY ;
    int startTouchViewLeft = 0;
    int startTouchViewTop = 0;
    private Context context;

    private int height;
    private int width;
    private int endX;
    private int endY;
    private int toWhere;

    float xDown,yDown,xUp;
    boolean isLongClickModule = false;
    boolean isLongClicking = false;

    public PictureTagLayout(Context context) {
        super(context, null);
    }
    public PictureTagLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context=context;

        init();
    }
    private void init(){

        //this.setOnTouchListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){

        }
    }

    public View addItem(int x, int y, String share, String brandId, int toWhere, String type){
        View view = null;
        LayoutParams params=new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        if(x>getWidth()*0.5){//Right是指 点在右边
            //第一次点击添加标签是  PictureTagView.Direction.Measure 让TagView自己测量方向   toWhere长按标签 向左滑动 还是向右
            view = new PictureTagView(getContext(), PictureTagView.Direction.Right,share,type);
            view.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
            int w = MeasureSpec.makeMeasureSpec(0,
                    MeasureSpec.UNSPECIFIED);
            int h = MeasureSpec.makeMeasureSpec(0,
                    MeasureSpec.UNSPECIFIED);
            view.measure(w, h);
            height = view.getMeasuredHeight();
            width = view.getMeasuredWidth();
            //标签在右面 点击位置 x-标签宽度   右边的标签并不是以圆点开始的  而是以左边的wei
            params.leftMargin = x -width+10;
            ((PictureTagView)view).addText(share);
            if (brandId!=null){
                if (!"0".equals(brandId)){//品牌id 默认为int 类型的  默认值为0  如果为0 证明是自定义的品牌  就不给品牌id 赋值
                    ((PictureTagView)view).addBrandId(brandId+"");
                }

            }
            //
            params.topMargin = y-height/2;
        }else{
            params.leftMargin = x-10;
            view = new PictureTagView(getContext(), PictureTagView.Direction.Left,share,type);
            view.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
            int w = MeasureSpec.makeMeasureSpec(0,
                    MeasureSpec.UNSPECIFIED);
            int h = MeasureSpec.makeMeasureSpec(0,
                    MeasureSpec.UNSPECIFIED);
            view.measure(w, h);
            height = view.getMeasuredHeight();
            width = view.getMeasuredWidth();
            ((PictureTagView)view).addText(share);
            if (brandId!=null){
                if ("0".equals(brandId)){//品牌id 默认为int 类型的  默认值为0  如果为0 证明是自定义的品牌  就不给品牌id 赋值
                    ((PictureTagView)view).addBrandId(brandId+"");
                }

            }
            params.topMargin = y-height/2;
        }
        //上下位置在视图内
        if(params.topMargin<=0){
            params.topMargin =0;
        } else if((params.topMargin+height)>getHeight()){
            params.topMargin = getHeight() -height;
        }
        if(params.leftMargin<=0){
            params.leftMargin=0;
        }
        if (params.rightMargin>=getWidth()){
            params.rightMargin=getWidth();
        }
        this.addView(view, params);
        return view;
    }

    public void setText(View touchView){
        //移动时将是向左滑动 还是向右滑动 赋值PictureTagView
        ((PictureTagView)touchView).setToWhere(toWhere);
    }

    public void moveView(int x,int y,View touchView){
        if(touchView == null) return;
        LayoutParams params=new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        height = touchView.getMeasuredHeight();
        width = touchView.getMeasuredWidth();
        //标签在右面 点击位置 x-标签宽度   右边的标签并不是以圆点开始的  而是以左边的wei
        params.leftMargin = x -width+10 - startX + startTouchViewLeft;
        params.topMargin = y -height+5 - startY + startTouchViewTop;
        //限制子控件移动必须在视图范围内
        if(params.leftMargin<0||(params.leftMargin+touchView.getWidth())>getWidth()){
            params.leftMargin = touchView.getLeft();
        }
        if(params.topMargin<0||(params.topMargin+touchView.getHeight())>getHeight()){
            params.topMargin = touchView.getTop();
        }
        touchView.setLayoutParams(params);
        //移动时将是向左滑动 还是向右滑动 赋值PictureTagView
        ((PictureTagView)touchView).setToWhere(toWhere);
    }

    public void setPostion(int x,int y,View touchView){
        if(touchView == null) return;
        LayoutParams params=new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        height = touchView.getMeasuredHeight();
        width = touchView.getMeasuredWidth();
        //标签在右面 点击位置 x-标签宽度   右边的标签并不是以圆点开始的  而是以左边的wei
        params.leftMargin = x -width+10;
        params.topMargin = y -height+5;
        touchView.setLayoutParams(params);
    }
    public View hasView(int x,int y){
        //循环获取子view，判断xy是否在子view上，即判断是否按住了子view
        for(int index = 0; index < this.getChildCount(); index ++){
            View view = this.getChildAt(index);
            int left = (int) view.getX();
            int top = (int) view.getY();
            int right = view.getRight();
            int bottom = view.getBottom();
            ((PictureTagView)view).getShare();
//			Toast.makeText(context, "已经有的---"+((PictureTagView)view).getShare()+"-x-"+left+"--y--"+top, Toast.LENGTH_SHORT).show();
            Rect rect = new Rect(left, top, right, bottom);
            boolean contains = rect.contains(x, y);
            //如果是与子view重叠则返回真,表示已经有了view不需要添加新view了
            if(contains){
                System.out.println("hasView-contains");
                return view;
            }

        }
        return null;
    }
    /* 判断是否有长按动作发生
* @param lastX 按下时X坐标
* @param lastY 按下时Y坐标
* @param thisX 移动时X坐标
* @param thisY 移动时Y坐标
* @param lastDownTime 按下时间
* @param thisEventTime 移动时间
* @param longPressTime 判断长按时间的阀值
*/
    public boolean isLongPressed(float lastX,float lastY,
                                  float thisX,float thisY,
                                  long lastDownTime,long thisEventTime,
                                  long longPressTime){
        float offsetX = Math.abs(thisX - lastX);
        float offsetY = Math.abs(thisY - lastY);
        long intervalTime = thisEventTime - lastDownTime;
        if(offsetX <= 10 && offsetY <= 10 && intervalTime >= longPressTime){
            return true;
        }
        return false;
    }
}
